package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionSq {
	//可以解压sqljdbc.jar看看Driver的path是什么   
	private String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private String url="jdbc:sqlserver://localhost:1433;databaseName=database1";
	 private String user="sa";
	private String pwd="sa"; 
	private Connection conn=null;
	
    public Connection getConnection(){
	try {
			Class.forName(Driver);
			try {
				conn= DriverManager.getConnection(url, user, pwd);
				 } catch (SQLException e) {
					 // TODO Auto-generated catch block             
e.printStackTrace();
					 }
					} catch (ClassNotFoundException e) {
						 // TODO Auto-generated catch block     
						 e.printStackTrace();
						}
						return conn;
			}
						public static void main(String[] args)
						{
							 ConnectionSq cs=new ConnectionSq();
							Connection c=cs.getConnection();
							 if (c!=null){
								 System.out.println("successful");
								 }
								try {
									Statement sl=c.createStatement();
									String sql="select * from dataTableName";
									ResultSet rs=sl.executeQuery(sql);
									while(rs.next()){
										 System.out.println(rs.getString("a"));
								}
										rs.close();
										 sl.close();
										c.close();
										} catch (SQLException e) {
											 // TODO Auto-generated catch block         
											 e.printStackTrace();
											 }
											 
						 }
	}

